import router from "../router/index";
import useLogin from "../stores/login";

// 创建白名单
const whiteList = ['/home','/deserve','/detail','/login','/loginpage','/register','/search','/category' ,'/:pathMatch(.*)']

// 路由前置守卫
router.beforeEach((to, from, next) => {
    let store = useLogin();
    const token = store.token
    console.log(token,to.path);
    //   如果有token
    if(token){
        if(to.path==='/login'){
            next('/')
        }else{
            next()
        }
    //   如果没有token
    }else{
        if(whiteList.includes(to.path)){
            next()
        }else{
            next('/login')
        }
    }
});

// 导出路由
export default router;
